package spingmvcstudent.service;

import org.springframework.beans.factory.annotation.Autowired;
import spingmvcstudent.Dao.UserDao;
import spingmvcstudent.Dao.UserDaoImpl;
import spingmvcstudent.pojo.User;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class  UserServiceImpl implements UserService {

    @Autowired
    UserDaoImpl userDao;

    @Override
    public List<User> getAllUser() {
       List<User> allUser = userDao.getAllUser();

       return allUser;
   }
@Override
   public int addUser( User user) {
       List<User> byName = userDao.findByName(user.getUsername());
       if (byName.size()>0){
           return 0;
       }else {
           int i = userDao.addUser(user);
           return i;
       }
   }
@Override
   public int deleteUser(Integer id) {
       int i = userDao.deleteUser(id);
       return i;
   }
@Override
   public List<User> findById(Integer id) {
       List<User> byId = userDao.findById(id);
       return byId;
   }
@Override
   public int updateUser(User user) {
       List<User> byIdAandName = userDao.findByIdAandName(user.getId(), user.getUsername());
       if (byIdAandName.size()>0){
           return 0;
       }else {
           int i = userDao.updateUser(user);
           return i;
       }
   }
@Override
   public String findByNameAndPassword(String username, String password) {
       List<User> byNameAndPassword = userDao.findByNameAndPassword(username, password);
       if (byNameAndPassword.size()>0){
           return "ok";
       }else {
           return "no";
       }
   }
}
